package com.xxr.mom.ci.charging.interworking.enums;

import cn.hutool.core.lang.Assert;
import com.xxr.mom.ci.common.exception.BusinessException;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @description: VersionEnum
 * @date: 2024/8/27 22:24
 * @author: natsuki_kining
 * @version: 1.0
 */
@Log4j2
@Getter
public enum VersionEnum {

    /**
     * <a href="https://cec.org.cn/menu/index.html?287">中电联</a>
     */
    V2016("V2016", "", "中电联2016版"),
    /**
     * <a href="https://support.huawei.com/enterprise/zh/doc/EDOC1100323030/80a59f47?togo=content">华为智能充电网络北向接口协议</a>
     */
    HW_V1("HuaWeiV1", "HuaWeiV1", "华为V1.x"),
    /**
     * <a href="https://open-energy.xiaojukeji.com/home/document/1100/262">小桔</a>
     */
    XIAO_JU("XiaoJu", "XiaoJu", "小桔"),

    ;
    private final String version;
    private final String desc;
    private final String serviceSuffix;

    VersionEnum(String version, String serviceSuffix, String desc) {
        this.version = version;
        this.serviceSuffix = serviceSuffix;
        this.desc = desc;
    }

    public static List<VersionEnum> versionEnumList() {
        VersionEnum[] values = VersionEnum.values();
        return Arrays.asList(values);
    }

    /**
     * 根据版本获取枚举，如果没有则返回空
     *
     * @param version 版本号
     * @return VersionEnum
     */
    public static VersionEnum getByVersion(String version) {
        for (VersionEnum versionEnum : versionEnumList()) {
            if (Objects.equals(versionEnum.getVersion(), version)) {
                return versionEnum;
            }
        }
        return null;
    }

    /**
     * 根据版本号获取serviceSuffix，如果没有则报错
     *
     * @param version
     * @return serviceSuffix
     */
    public static String getServiceSuffix(String version) {
        VersionEnum versionEnum = getByVersion(version);
        Assert.notNull(versionEnum, () -> {
            log.error("互联互通配置接口版本找不到对应的枚举：{}", version);
            throw new BusinessException(500, "根据" + version + "找不到对应的枚举数据");
        });
        return versionEnum.getServiceSuffix();
    }
}
